/* emloyeetest.java 
* Created on Dec 20, 2012 
* Author:Mr.Viet
* Phone: 01683430426
* Email: phamvanvietcdhd@gmail.com
*/ 
package com.test; 

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;

import com.icode.utils.DBconnecttion;

public class emloyeetest {

	public emloyeetest() {
		
	}
	
	public static void main(String[] args) {
		emloyeetest t = new emloyeetest();
//		t.testPreStatement();
		t.testStatement();
	}

	public void testPreStatement(){
		Connection conn = DBconnecttion.getConnection();
		String sql = "SELECT * FROM Emloyee WHERE id>?";
		try {
			PreparedStatement st = conn.prepareStatement(sql);
			st.setInt(1, 0);
			ResultSet rs = st.executeQuery();
			while(rs.next()){
				String name = rs.getString("name");
				System.out.println(name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void testStatement(){
		Connection conn = DBconnecttion.getConnection();
		try {
			Statement st = conn.createStatement();
			
			//INSERT
			String sql = "INSERT INTO Emloyee values('Danghh','HN','IT')";
//			st.executeUpdate(sql);
//			//Uplate
			sql = "UPDATE Emloyee set name='Tam' where id=1 ";
			st.executeUpdate(sql);
//			//Delete 
			sql="DELETE FROM Emloyee where name ='danghh'";
			st.executeUpdate(sql);
			//Lay Du Lieu
			ResultSet rs = st.executeQuery("SELECT * FROM Emloyee");
			while(rs.next()){
				String name = rs.getString("name");
				System.out.println(name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
